# -*- coding: utf-8 -*-

###########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

# 商户管理
# Merchant management

from flask import Blueprint
from flask import request
import utils.pgsql.pgseller as seller
from utils.wraps import token, urlcost
import utils.message.response as response
import json



seller_module = Blueprint('seller_sellerment', __name__,url_prefix='/api/v1/seller/sellerment')


@seller_module.route('/', methods=['POST'])
@token.login_required
@urlcost.cost_count_wraps
def point_add():

    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])
    if userInfo['role'] in [1, 2, 3]:
        obj = seller.PgSeller()
        dic = json.loads(request.data)

        if dic['firmName'] != "":
            if obj.firmNameget(dic['firmName']) == []:
                res = obj.addseller(dic)
                return response.make_response_success(res)
            else:
                return response.make_response_1505()
        else:
            res = obj.addseller(dic)
            return response.make_response_success(res)
    else:
        return response.make_response_401()


@seller_module.route('/<int:sellerId>', methods=['PUT'])
@token.login_required
@urlcost.cost_count_wraps
def point_put(sellerId):

    # tokenInfo = request.headers["Authorization"]
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])
    if userInfo['role'] in [2, 1, 3]:
        obj = seller.PgSeller()
        dic = json.loads(request.data)
        if obj.accounttypeget(sellerId)[0]['accounttype'] == '企业账号':
            if "firmName" in dic.keys():
                if obj.firmNameeditget(dic['firmName'], sellerId) == []:
                    if obj.modify(sellerId, dic):
                        return response.make_response_success_no_data()
                    else:
                        return response.make_response_1000()
                else:
                    return response.make_response_1505()
            else:
                if obj.modify(sellerId, dic):
                    return response.make_response_success_no_data()
                else:
                    return response.make_response_1000()
        else:
            if obj.modify(sellerId, dic):
                return response.make_response_success_no_data()
            else:
                return response.make_response_1000()
    else:
        return response.make_response_401()


@seller_module.route('/audit', methods=['GET'])
@token.login_required
@urlcost.cost_count_wraps
def seller_all_audit():

    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo['role'] in [1, 2, 3]:
        obj = seller.PgSeller()
        return response.make_response_success(obj.allByauditatatus(1))
    else:
        return response.make_response_401()


@seller_module.route('/admin/<int:adminId>', methods=['GET'])
@token.login_required
@urlcost.cost_count_wraps
def seller_all_admin(adminId):

    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    obj = seller.PgSeller()
    if userInfo['role'] == 1:
        return response.make_response_success(obj.all())
    elif userInfo['role'] == 2:
        operatorId = userInfo['operatorId']
        return response.make_response_success(obj.allByoperator(operatorId))
    elif userInfo['role'] == 3:
        caseId = userInfo['caseId']
        return response.make_response_success(obj.allBycase(caseId))
    # elif userInfo['role'] == 3:
    #     return response.make_response_success(obj.allByAdminRole3(adminId))
    else:
        return response.make_response_401()


@seller_module.route('/uncase/<int:operatorId>', methods=['GET'])
@token.login_required
@urlcost.cost_count_wraps
def seller_uncase(operatorId):

    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    obj = seller.PgSeller()

    if userInfo['role'] == 2:
        operatorId = userInfo['operatorId']
        return response.make_response_success(obj.allunusecase(operatorId))
    elif userInfo['role'] == 3:
        operatorId = userInfo['caseId']
        return response.make_response_success(obj.allunusecasecaseid(operatorId))
    else:
        return response.make_response_401()


@seller_module.route('/<int:operatorId>', methods=['GET'])
@token.login_required
@urlcost.cost_count_wraps
def point_all_case(operatorId):


    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo['role'] in [1, 2]:
        obj = seller.PgSeller()
        return response.make_response_success(obj.allByoperator(operatorId))
    else:
        return response.make_response_401()


@seller_module.route('/<int:sellerId>', methods=['DELETE'])
@token.login_required
@urlcost.cost_count_wraps
def point_delete(sellerId):

    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo['role'] in [2, 1, 3]:
        obj = seller.PgSeller()
        if(obj.allSeller(sellerId)[0]['accounttype'] == '企业账号'):
            if obj.deletefirm(obj.allSeller(sellerId)[0]['caseId']):
                return response.make_response_success_no_data()
            else:
                return response.make_response_1000()
        else:
            if obj.delete(sellerId):
                return response.make_response_success_no_data()
            else:
                return response.make_response_1000()
    else:
        return response.make_response_401()
    
@seller_module.route("/sellerall", methods=["GET"])
@urlcost.cost_count_wraps
def seller_all():
  
    obj = seller.PgSeller()
    res = obj.all()
    if(res):
        return response.make_response_success(res)
    else:
        return response.make_response_1000()

 
@seller_module.route("/case/sellerall/<int:caseId>", methods=["GET"])
@urlcost.cost_count_wraps
def seller_case_all(caseId):

    obj = seller.PgSeller()
    res =  obj.allBycase(caseId)
    if(res):
        return response.make_response_success(res)
    else:
        return response.make_response_1000()
